package cn.com.haopy.dataes.db.datasource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author keel
 */
@Component
@Order(-100)
@Aspect
public class DataSourceAspect {
    private final Log logger = LogFactory.getLog(DataSourceContextHolder.class);

    @Pointcut("execution(* cn.com.haopy.dataes.db.service.main..*(..))")
    private void dbMainAspect() {
        //
    }

    @Pointcut("execution(* cn.com.haopy.dataes.db.service.service..*(..))")
    private void dbServiceAspect() {
        //
    }

    @Before("dbMainAspect()")
    public void dbMain() {
        logger.info("switch datasource to main");
        DataSourceContextHolder.setDbType(DBTypeEnum.MAIN);
    }

    @Before("dbServiceAspect()")
    public void dbService() {
        logger.info("switch datasource to service");
        DataSourceContextHolder.setDbType(DBTypeEnum.SERVICE);
    }
}
